package org.dromara.hm.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.hm.domain.vo.HmPaymentAdjustmentVo;
import org.dromara.hm.domain.bo.HmPaymentAdjustmentBo;
import org.dromara.hm.service.IHmPaymentAdjustmentService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 回款调整记录
 *
 * @author Claude Code
 * @date 2025-01-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/hm/paymentAdjustment")
public class HmPaymentAdjustmentController extends BaseController {

    private final IHmPaymentAdjustmentService paymentAdjustmentService;

    /**
     * 查询回款调整记录列表
     */
    @GetMapping("/list")
    public TableDataInfo<HmPaymentAdjustmentVo> list(HmPaymentAdjustmentBo bo, PageQuery pageQuery) {
        return paymentAdjustmentService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出回款调整记录列表
     */
    @Log(title = "回款调整记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HmPaymentAdjustmentBo bo, HttpServletResponse response) {
        List<HmPaymentAdjustmentVo> list = paymentAdjustmentService.queryList(bo);
        ExcelUtil.exportExcel(list, "回款调整记录", HmPaymentAdjustmentVo.class, response);
    }

    /**
     * 获取回款调整记录详细信息
     *
     * @param adjustmentId 主键
     */
    @GetMapping("/{adjustmentId}")
    public R<HmPaymentAdjustmentVo> getInfo(@NotNull(message = "主键不能为空")
                                            @PathVariable Long adjustmentId) {
        return R.ok(paymentAdjustmentService.queryById(adjustmentId));
    }

    /**
     * 申请回款调整
     */
    @Log(title = "回款调整申请", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/apply")
    public R<Void> apply(@Validated(AddGroup.class) @RequestBody HmPaymentAdjustmentBo bo) {
        return toAjax(paymentAdjustmentService.applyAdjustment(bo));
    }

    /**
     * 审核回款调整（批准）
     *
     * @param adjustmentId  调整记录ID
     * @param approveRemark 审核备注
     */
    @Log(title = "回款调整审核", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/approve/{adjustmentId}")
    public R<Void> approve(@NotNull(message = "调整记录ID不能为空") @PathVariable Long adjustmentId,
                           @RequestParam(required = false) String approveRemark) {
        return toAjax(paymentAdjustmentService.approveAdjustment(adjustmentId, approveRemark));
    }

    /**
     * 驳回回款调整
     *
     * @param adjustmentId  调整记录ID
     * @param rejectReason  驳回原因
     */
    @Log(title = "回款调整驳回", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/reject/{adjustmentId}")
    public R<Void> reject(@NotNull(message = "调整记录ID不能为空") @PathVariable Long adjustmentId,
                          @RequestParam String rejectReason) {
        return toAjax(paymentAdjustmentService.rejectAdjustment(adjustmentId, rejectReason));
    }

    /**
     * 取消回款调整
     *
     * @param adjustmentId 调整记录ID
     */
    @Log(title = "回款调整取消", businessType = BusinessType.UPDATE)
    @PostMapping("/cancel/{adjustmentId}")
    public R<Void> cancel(@NotNull(message = "调整记录ID不能为空") @PathVariable Long adjustmentId) {
        return toAjax(paymentAdjustmentService.cancelAdjustment(adjustmentId));
    }

    /**
     * 修改回款调整记录
     */
    @Log(title = "回款调整记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HmPaymentAdjustmentBo bo) {
        return toAjax(paymentAdjustmentService.updateByBo(bo));
    }

    /**
     * 删除回款调整记录
     *
     * @param adjustmentIds 主键串
     */
    @Log(title = "回款调整记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{adjustmentIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] adjustmentIds) {
        return toAjax(paymentAdjustmentService.deleteWithValidByIds(List.of(adjustmentIds), true));
    }
}
